package com.example.understand_compose1.testcase_activity

import android.app.Activity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Bundle
import android.util.Log
import com.example.understand_compose1.R

class FetBoardCastActivity: Activity() {

    private val batteryLevelReceiver = BatteryLevelReceiver()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.plain_login)

        registerReceiver(batteryLevelReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(batteryLevelReceiver)
    }
}

// 广播测试
class BatteryLevelReceiver : BroadcastReceiver() {
    // 监听电量
    override fun onReceive(context: Context, intent: Intent) {
        val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
        val scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
        val batteryPct = level * 100 / scale.toFloat()
        // 在logcat里搜索Battery level
        Log.d("BatteryLevelReceiver", "Battery level: $batteryPct%")
    }
}